from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession

from src.application.server.enums import ServerPrivacy, ServerStatus, ServerType
from src.infrastructure.db.models import Country, Server


async def get_country(session: AsyncSession, country_id: int) -> Country:
    q = select(Country).where(Country.id == country_id)
    res = await session.execute(q)

    return res.scalar()


async def query_countries(session: AsyncSession, proxy_type: ServerType, proxy_privacy: ServerPrivacy) -> list[Country]:
    q = (
        select(Country)
        .join(Country.servers)
        .filter(
            and_(
                Server.status == ServerStatus.active,
                Server.proxy_type == proxy_type,
                Server.proxy_privacy == proxy_privacy,
            )
        )
    )

    res = await session.execute(q)

    return res.scalars().unique().all()
